package view.menu;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JTextField;
import javax.swing.JLabel;
import java.awt.Font;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.JButton;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

import com.jgoodies.common.swing.MnemonicUtils;

import dao.MenuDao;
import entiey.Dictionary;
import entiey.Menu;
import entiey.OrderFood;
import service.DictionaryService;
import service.MenuService;
import service.impl.DictionaryServiceImpl;
import service.impl.MenuServiceImpl;

import javax.swing.JPopupMenu;
import java.awt.Component;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.border.LineBorder;
import java.awt.Color;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
/**
 * 菜单管理页面
 * @author 赵红娇
 * view.menu
 * @date 2020年8月30日下午5:40:20
 */
public class V_menuManage extends JFrame {

	private JPanel contentPane;
	private JTable table;
	private JTextField text_input;
	private JButton btn_find;
	private MenuService ms=new MenuServiceImpl();

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					V_menuManage frame = new V_menuManage();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public V_menuManage() {
		setTitle("菜单管理");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 821, 527);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		
		JScrollPane scrollPane = new JScrollPane();
		
		JLabel lblNewLabel = new JLabel("编号/菜名/拼音简写：");
		lblNewLabel.setFont(new Font("宋体", Font.PLAIN, 14));
		
		text_input = new JTextField();
		text_input.setColumns(10);
		
		btn_find = new JButton("查询");
		btn_find.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String text = text_input.getText();
				List<Menu> list = ms.likeFind(text);
				System.out.println(list.size());
				inittable(list);
			}
		});
		
		JButton btn_add = new JButton("添加新菜品");
		btn_add.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				V_menuAdd menuAdd=new V_menuAdd();
				menuAdd.setBounds(V_menuManage.this.getX()+200, V_menuManage.this.getY()+200, menuAdd.getWidth(),menuAdd.getHeight());
				menuAdd.setVisible(true);
				menuAdd.addWindowListener(new WindowAdapter() {
					@Override
					public void windowClosed(WindowEvent e) {
						//添加串口关闭，初始化table
						inittable(ms.findAll());
					}
				});
			}
		});
		GroupLayout gl_contentPane = new GroupLayout(contentPane);
		gl_contentPane.setHorizontalGroup(
			gl_contentPane.createParallelGroup(Alignment.TRAILING)
				.addGroup(gl_contentPane.createSequentialGroup()
					.addGap(6)
					.addComponent(scrollPane, GroupLayout.DEFAULT_SIZE, 786, Short.MAX_VALUE)
					.addGap(5))
				.addGroup(gl_contentPane.createSequentialGroup()
					.addGap(359)
					.addComponent(lblNewLabel)
					.addPreferredGap(ComponentPlacement.UNRELATED)
					.addComponent(text_input, GroupLayout.PREFERRED_SIZE, 116, GroupLayout.PREFERRED_SIZE)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addComponent(btn_find, GroupLayout.PREFERRED_SIZE, 74, GroupLayout.PREFERRED_SIZE)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addComponent(btn_add)
					.addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
		);
		gl_contentPane.setVerticalGroup(
			gl_contentPane.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_contentPane.createSequentialGroup()
					.addGap(41)
					.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
						.addComponent(btn_add)
						.addGroup(gl_contentPane.createSequentialGroup()
							.addGap(1)
							.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
								.addComponent(text_input, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
								.addComponent(lblNewLabel, GroupLayout.PREFERRED_SIZE, 16, GroupLayout.PREFERRED_SIZE)))
						.addComponent(btn_find))
					.addGap(18)
					.addComponent(scrollPane, GroupLayout.DEFAULT_SIZE, 382, Short.MAX_VALUE)
					.addGap(16))
		);
		
		table = new JTable();
		table.setModel(new DefaultTableModel(
			new Object[][] {
			},
			new String[] {
				"\u7F16\u53F7", "\u83DC\u540D", "\u62FC\u97F3\u7B80\u5199", "\u5355\u4EF7", "\u83DC\u54C1\u7C7B\u578B", "\u5546\u54C1\u72B6\u6001"
			}
		));
		scrollPane.setViewportView(table);
		
		JPopupMenu popupMenu = new JPopupMenu();
		addPopup(table, popupMenu);
		
		JMenuItem mntmNewMenuItem = new JMenuItem("修改");
		mntmNewMenuItem.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				int selectedRow = table.getSelectedRow();
				int id = (int) table.getValueAt(selectedRow, 0);
				Menu menu = ms.findById(id);
				V_menuAdd add=new V_menuAdd();
				//修改初始化页面
				add.init(menu);
				add.setBounds(V_menuManage.this.getX(), V_menuManage.this.getY(), add.getWidth(), add.getHeight());
				add.setVisible(true);
				add.addWindowListener(new WindowAdapter() {
					@Override
					public void windowClosed(WindowEvent e) {
						inittable(ms.findAll());
					}
				});
			}
		});
		popupMenu.add(mntmNewMenuItem);
		
		JMenuItem mntmNewMenuItem_1 = new JMenuItem("删除");
		mntmNewMenuItem_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				int selectedRow = table.getSelectedRow();
				int id = (int) table.getValueAt(selectedRow, 0);
				if(ms.delete(id)) {
					JOptionPane.showMessageDialog(V_menuManage.this, "菜品删除成功","成功提示",JOptionPane.INFORMATION_MESSAGE);
					inittable(ms.findAll());
				}else {
					JOptionPane.showMessageDialog(V_menuManage.this, "菜品删除失败","失败提示",JOptionPane.ERROR_MESSAGE);
				}
			}
		});
		popupMenu.add(mntmNewMenuItem_1);
		contentPane.setLayout(gl_contentPane);
		
		inittable(ms.findAll());
	}
	private static void addPopup(Component component, final JPopupMenu popup) {
		component.addMouseListener(new MouseAdapter() {
			public void mousePressed(MouseEvent e) {
				if (e.isPopupTrigger()) {
					showMenu(e);
				}
			}
			public void mouseReleased(MouseEvent e) {
				if (e.isPopupTrigger()) {
					showMenu(e);
				}
			}
			private void showMenu(MouseEvent e) {
				popup.show(e.getComponent(), e.getX(), e.getY());
			}
		});
	}
	/**
	 * 初始化表格
	 */
	public void inittable(List<Menu> list) {
		DefaultTableModel dtm=(DefaultTableModel) table.getModel();
		int rowCount = dtm.getRowCount();
		for (int i = 0; i < rowCount; i++) {
			dtm.removeRow(0);
		}
		//初始化表格
		for (Menu menu : list) {
			String typeString="";
			//查询字典表，得到菜品类型
			String status="";
			if(menu.getStatus()==1) {
				status="出售中";
			}else {
				status="已下架";
			}
			//根据数据库中的类型id查找出字典中相应id的菜品类型名称
			DictionaryService ds=new DictionaryServiceImpl();
			String type = null;
			if(menu.getType()>0) {
				Dictionary dictionaryById = ds.getDictionaryById(menu.getType());
				type=dictionaryById.getName();
			}else{
				type="本菜品未分类";
			}
			dtm.addRow(new Object[] {
				menu.getId(),
				menu.getFoodName(),
				menu.getSpellCode(),
				menu.getFoodPrice(),
				type,//菜品类型
				status
			});
		}
	}
}
